﻿using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Read.API.Command.UserCommand;
using MediatR;

namespace B.S.BaseData.Read.API.CommandHandler.UserHandler
{
    public class UserLoginHandler : IRequestHandler<UserLoginCommand, APIResult<User>>
    {
        private readonly IBaseRepository<User> _baseRepository;

        public UserLoginHandler(IBaseRepository<User> baseRepository)
        {
            _baseRepository = baseRepository;
        }

        public async Task<APIResult<User>> Handle(UserLoginCommand request, CancellationToken cancellationToken)
        {
            APIResult<User> result = new APIResult<User>();
            result.Code = APIEnum.Success;
            result.Msg = "登录成功";

            var list = _baseRepository.GetAll().FirstOrDefault(p=>p.Name==request.Name);
            if(list==null)
            {
                result.Code = APIEnum.Failed;
                result.Msg = "账号不存在";
                return await Task.FromResult(result);
            }
            if(list.Pass!=request.Pass)
            {
                result.Code = APIEnum.Failed;
                result.Msg = "账号密码不匹配";
                return await Task.FromResult(result);
            }

            result.Data = list;
            return await Task.FromResult(result);
        }
    }
}
